package org.tis.tools.abf.module.ac.controller;

import com.baomidou.mybatisplus.plugins.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.tis.tools.abf.module.ac.controller.request.*;
import org.tis.tools.abf.module.ac.entity.AcRole;
import org.tis.tools.abf.module.ac.service.IAcRoleFuncService;
import org.tis.tools.abf.module.ac.service.IAcRoleService;
import org.tis.tools.core.web.controller.BaseController;
import org.tis.tools.core.web.vo.SmartPage;
import org.tis.tools.model.common.ResultVO;
import org.tis.tools.model.log.enums.OperateType;
import org.tis.tools.starter.operatelog.annotation.OperateLog;

import javax.validation.constraints.NotBlank;

/**
 * acRole的Controller类
 * 
 * @author Auto Generate Tools
 * @date 2018/04/23
 */
@RestController
@RequestMapping("/acRoles")
@Validated
@Slf4j
public class AcRoleController extends BaseController<AcRole>  {

    @Autowired
    private IAcRoleService acRoleService;
    @Autowired
    IAcRoleFuncService acRoleFuncService;


    @ApiOperation(value = "分页查询角色信息",notes = "需要传入page,condition作为参数")
    @PostMapping("/list")
    public ResultVO queryRoleWithPage(@RequestBody @Validated @ApiParam(value = "page",name = "分页信息(page)", required = true) SmartPage<AcRole> page) {
        Page page1  =  acRoleService.selectPage(page.getPageObj(),page.getConditionWrapper());
        return  ResultVO.success("查询成功", page1);
    }


    @ApiOperation(value = "根据角色代码查询角色信息",notes = "需要传入角色代码(roleCode)作为参数")
    @GetMapping("/{roleCode}")
    public ResultVO selectByRoleCode(@PathVariable @NotBlank(message = "roleCode不能为空") @ApiParam(value = "roleCode",name = "角色代码", required = true) String roleCode) {
        AcRole acRole = new AcRole();
        acRole.setRoleCode(roleCode);
        AcRole acRole1 = acRoleService.queryByCondition(acRole);
        if (acRole1 == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        return ResultVO.success("查询成功", acRole1);
    }


    @ApiOperation(value = "根据id查询角色信息",notes = "需要传入角色GUID(id)作为参数")
    @GetMapping("/queryRoleById/{id}")
    public ResultVO selectById(@PathVariable @NotBlank(message = "GUID不能为空") @ApiParam(value = "id",name = "角色GUID",
            required = true) String id) {
        AcRole acRole = acRoleService.selectById(id);
        if (acRole == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        return ResultVO.success("查询成功", acRole);
    }


    @ApiOperation(value = "新增角色信息",notes = "需要传入AcRoleAddRequest类作为参数")
    @OperateLog(type = OperateType.ADD, desc = "新增角色")
    @PostMapping
    public ResultVO add(@RequestBody @Validated @ApiParam(value = "AcRoleAddRequest",name = "传入的AcRoleAddRequest类", required = true) AcRoleAddRequest request) {
        //判断角色代码是否已经存在
        if (acRoleService.existRoleCode(request.getRoleCode())){
            return ResultVO.error("404", "角色代码已经存在!");
        }
        AcRole acRole = acRoleService.createAcRole(request);
        return ResultVO.success("新增成功",acRole);
    }


    @ApiOperation(value = "修改角色信息",notes = "需要传入AcRole类作为参数")
    @OperateLog(type = OperateType.UPDATE, desc = "根据角色ID修改角色")
    @PutMapping("/update")
    public ResultVO update(@RequestBody @Validated({AcRoleUpdateValidateGrop.class}) @ApiParam(value = "AcRole",name = "传入的AcRole类", required = true) AcRole acRole) {
        AcRole acRole1 = acRoleService.selectById(acRole.getGuid());
        if (acRole1 == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        if (acRoleService.existUpdateRoleCode(acRole.getRoleCode(),acRole.getGuid())){
            return ResultVO.error("404", "角色代码已经存在!");
        }

        acRoleService.updateAcRole(acRole);
        return ResultVO.success("修改成功！",acRole);

    }


    @ApiOperation(value = "根据ID删除角色",notes = "需要传入角色GUID(id)作为参数")
    @OperateLog(type = OperateType.DELETE, desc = "根据ID删除角色")
    @DeleteMapping("/{id}")
    public ResultVO deleteRoleByRoleCode(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(value = "id",name = "角色GUID",
            required = true) String id) {
        AcRole acRole = acRoleService.selectById(id);
        if (acRole == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        acRoleService.deleteByRoleCode(id);
        return ResultVO.success("删除成功",acRole);
    }


    @ApiOperation(value = "批量删除角色结果",notes = "需要传入AcDeleteListRequest类作为参数")
    @OperateLog(type = OperateType.DELETE, desc = "批量删除角色")
    @PostMapping("/batchDelete")
    public ResultVO batchDelete(@RequestBody @Validated @ApiParam(value = "AcDeleteListRequest",name = "传入的AcDeleteListRequest类", required = true) AcDeleteListRequest listRequest) {
        acRoleService.batchDelete(listRequest);
        return ResultVO.success("删除成功");
    }


    @ApiOperation(value = "查询角色树形结构",notes = "不需要传入参数")
    @GetMapping("/roleTree")
    public ResultVO roleTree(){
        return ResultVO.success("查询成功",acRoleService.queryRoleTree());
    }


    @ApiOperation(value = "根据姓名查询角色树结构",notes = "需要传入AcRoleTreeFilterRequest类参数")
    @PostMapping("/roleTreeFilter")
    public ResultVO roleTreeFilter(@RequestBody @Validated @ApiParam(value = "AcRoleTreeFilterRequest",name = "传入的AcRoleTreeFilterRequest", required = true) AcRoleTreeFilterRequest treeFilterRequest){
        return ResultVO.success("查询成功",acRoleService.queryRoleTreeFilter(treeFilterRequest));
    }


    @ApiOperation(value = "根据角色分组按需查询角色列表",notes = "需要传入AcRoleQueryRequest类参数")
    @PostMapping("/queryRolesByGroup")
    public ResultVO queryRolesByGroup(@RequestBody @Validated @ApiParam(value = "AcRoleQueryRequest",name = "传入的AcRoleQueryRequest", required = true) AcRoleQueryRequest acRoleQueryRequest){
        return ResultVO.success("查询成功",acRoleService.queryRolesByGroup(acRoleQueryRequest.getRoleGroup()));
    }


    @ApiOperation(value = "根据条件查询操作员下的角色(分页)",notes = "需要传入分页信息和操作员GUID参数")
    @PostMapping("/queryByOperator/{operatorId}")
    public ResultVO queryRoleByOperator(@RequestBody @Validated @ApiParam(value = "page",name = "分页信息", required = true) SmartPage<AcRole> page, @PathVariable @NotBlank(message = "operatorId不能为空") @ApiParam(value = "operatorId",name = "操作员的GUID", required = true) String operatorId){
        return ResultVO.success("查询成功",acRoleService.queryByOperator(operatorId, page.getPageObj(), page.getConditionWrapper()));
    }


    @ApiOperation(value = "启用角色",notes = "传入需要启用角色的id参数")
    @PutMapping("/enableRole/{id}")
    public ResultVO enableRole(@PathVariable @NotBlank(message = "角色ID不能为空") @ApiParam(name = "id",value = "角色的guid",
            required = true) String id){
        return ResultVO.success("启用成功",acRoleService.enableRole(id));
    }


    @ApiOperation(value = "停用角色",notes = "传入需要停用角色的id参数")
    @PutMapping("/disableRole/{id}")
    public ResultVO disableRole(@PathVariable @NotBlank(message = "角色ID不能为空") @ApiParam(name = "id",value = "角色的guid",
            required = true) String id){
        return ResultVO.success("停用成功",acRoleService.disableRole(id));
    }


    @ApiOperation(value = "判断角色代码是否已经存在",notes = "传入角色代码(roleCode)参数")
    @GetMapping("/existRoleCode/{roleCode}")
    public ResultVO existRoleCode(@PathVariable @NotBlank(message = "角色代码不能为空") @ApiParam(name = "roleCode",value =
            "角色代码",required = true) String roleCode){
        return ResultVO.success("查询成功",acRoleService.existRoleCode(roleCode));
    }

    @ApiOperation(value = "判断修改时角色代码是否已经存在",notes = "传入角色代码(roleCode)和GUID(id)参数")
    @GetMapping("/{id}/existUpdateRoleCode/{roleCode}")
    public ResultVO existUpdateRoleCode(@PathVariable @NotBlank(message = "角色代码不能为空") @ApiParam(name = "roleCode",value = "角色代码",required = true) String roleCode ,@PathVariable @NotBlank(message = "GUID不能为空") @ApiParam(name = "id",value = "角色GUID",required = true) String id){
        return ResultVO.success("查询成功",acRoleService.existUpdateRoleCode(roleCode,id));
    }

    @ApiOperation(value = "角色移动",notes = "传入AcRoleMoveRequest类")
    @PostMapping("/moveRole")
    public ResultVO moveRole(@RequestBody @Validated @ApiParam(name = "AcRoleMoveRequest",value = "传入的AcRoleMoveRequest类",required = true) AcRoleMoveRequest acRoleMoveRequest){
        acRoleService.moveRoleToGroup(acRoleMoveRequest);
        return ResultVO.success("角色移动成功!");
    }
}

